/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewMDIApplication.java
 *
 * Created on 28/12/2010, 23:08:21
 */
package net.stuffrepos.bricknetcenter.gui;

import java.awt.EventQueue;
import java.beans.PropertyVetoException;
import javax.swing.JOptionPane;
import net.stuffrepos.bricknetcenter.App;
import net.stuffrepos.bricknetcenter.gui.informations.InformationsFrame;
import net.stuffrepos.bricknetcenter.gui.inventories.InventoriesFrame;
import net.stuffrepos.bricknetcenter.http.HttpDownloader;
import net.stuffrepos.bricknetcenter.provider.HttpProviderManager;
import net.stuffrepos.util.cache.CacheableValue;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Eduardo H. Bogoni <eduardobogoni@gmail.com>
 */
public class MainFrame extends javax.swing.JFrame {

    private static final Log log = LogFactory.getLog(MainFrame.class);
    private static final CacheableValue<MainFrame> INSTANCE = new CacheableValue<MainFrame>() {

        @Override
        protected MainFrame calculate() {
            return new MainFrame();
        }
    };

    public static MainFrame getInstance() {
        return INSTANCE.getValue();
    }

    private MainFrame() {
        super("Brick Net Center");
        initComponents();
        setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                openNewInventoriesWindow();
                //openNewInformationsWindow();
            }
        });
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        desktopPane = new javax.swing.JDesktopPane();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        openNewInventoriesWindow = new javax.swing.JMenuItem();
        openNewInformationsWindow = new javax.swing.JMenuItem();
        exit = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        fileMenu.setText("File");

        openNewInventoriesWindow.setText("Open New Inventories Window");
        openNewInventoriesWindow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openNewInventoriesWindowActionPerformed(evt);
            }
        });
        fileMenu.add(openNewInventoriesWindow);

        openNewInformationsWindow.setText("Open New Informations Window");
        openNewInformationsWindow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openNewInformationsWindowActionPerformed(evt);
            }
        });
        fileMenu.add(openNewInformationsWindow);

        exit.setText("Exit");
        exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });
        fileMenu.add(exit);

        menuBar.add(fileMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(desktopPane, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(desktopPane, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed
        System.exit(0);
    }//GEN-LAST:event_exitActionPerformed

    private void openNewInventoriesWindow() {
        try {
            InventoriesFrame inventoriesFrame = new InventoriesFrame(this);
            desktopPane.add(inventoriesFrame);
            inventoriesFrame.pack();
            try {
                inventoriesFrame.setMaximum(true);
            } catch (PropertyVetoException ex) {
                log.warn("Exception on maximize internal frame", ex);
            }
            inventoriesFrame.setVisible(true);
            this.pack();
        } catch (Exception ex) {
            registerException("Exception on opening window", ex);
        }
    }

    private void openNewInformationsWindow() {
        try {
            InformationsFrame informationsFrame = new InformationsFrame();
            desktopPane.add(informationsFrame);
            informationsFrame.pack();
            try {
                informationsFrame.setMaximum(true);
            } catch (PropertyVetoException ex) {
                log.warn("Exception on maximize internal frame", ex);
            }
            informationsFrame.setVisible(true);
            this.pack();
        } catch (Exception ex) {
            registerException("Exception on opening window", ex);
        }
    }

    private void openNewInventoriesWindowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openNewInventoriesWindowActionPerformed
        openNewInventoriesWindow();
    }//GEN-LAST:event_openNewInventoriesWindowActionPerformed

    private void openNewInformationsWindowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openNewInformationsWindowActionPerformed
        openNewInformationsWindow();
    }//GEN-LAST:event_openNewInformationsWindowActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        App.close();
    }//GEN-LAST:event_formWindowClosing
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDesktopPane desktopPane;
    private javax.swing.JMenuItem exit;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem openNewInformationsWindow;
    private javax.swing.JMenuItem openNewInventoriesWindow;
    // End of variables declaration//GEN-END:variables

    public void registerException(String message, Exception ex) {
        log.warn(message, ex);
        JOptionPane.showMessageDialog(rootPane, message);
    }

    public void registerException(Exception ex) {
        registerException("Exception", ex);
    }

    public void invokeLater(final Runnable runnable) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    runnable.run();
                } catch (Exception ex) {
                    registerException(ex);
                }
            }
        });
    }
}
